package com.ayoka.http;

import com.ayoka.base.BasePresenter;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.CouponBean;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.bean.CourseBean;
import com.ayoka.bean.CoursePackageBean;
import com.ayoka.bean.PagerBean;
import com.ayoka.bean.ResponseCoursePackageDetailBean;
import com.ayoka.bean.ResponseRefreshTokenBean;
import com.ayoka.bean.TrainingPackageBean;
import com.ayoka.bean.UserInfoBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.Constant;
import com.ayoka.bean.WechatPaymentBean;
import com.ayoka.mvp.CourseAssignmentVideoMVP;
import com.ayoka.mvp.CourseEstimateMVP;
import com.ayoka.mvp.CourseEstimateQuestionMVP;
import com.ayoka.mvp.CourseMVP;
import com.ayoka.mvp.CourseQuestionMVP;
import com.ayoka.mvp.PurchaseRecordMVP;
import com.ayoka.mvp.TrainingDetailMVP;
import com.ayoka.mvp.TrainingGroupMVP;
import com.jcp.baselibrary.okhttp3.OkHttpUtil;
import com.jcp.baselibrary.okhttp3.OkRequestParams;
import com.jcp.baselibrary.okhttp3.RequestBean;
import com.jcp.baselibrary.okhttp3.response.OkHttpResFileHandler;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * =========================================================
 * 作者： 金春平
 * 类名： LoginPresenter
 * 类描述：
 * 创建日期 17/09/04
 * =========================================================
 */
public class API {

    public static void downloadFile(String url, OkHttpResFileHandler callBack) {
        OkHttpUtil.get(url, "fileDown", callBack);
    }

    /**
     * 获取当前用户数
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getJoinCount(BasePresenter presenter, OkHttpListener<BaseResponse<Long>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/Login/UsersCount";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 登录
     *
     * @param presenter
     * @param code
     * @param okHttpListener
     */
    public static void doLoginByWX(BasePresenter presenter, String code, OkHttpListener<BaseResponse<UserInformation>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/Login/Login";
        OkRequestParams params = new OkRequestParams();
        params.put("code", code);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 刷新token
     *
     * @param ID
     * @param refresh_token
     * @param okHttpCallback
     */
    public static void refreshToken(int ID, String refresh_token, OkHttpListener<BaseResponse<ResponseRefreshTokenBean>> okHttpCallback) {
        String url = Constant.getBaseApiUrl() + "User/Login/RefreshToken";
        OkRequestParams params = new OkRequestParams();
        params.putJson("Uid", String.valueOf(ID));
        params.putJson("RefreshToken", refresh_token);
        Call call = null;
        Callback callback = OkHttpUtil.getCallBack(okHttpCallback);
        try {
            RequestBody requestBody = OkHttpUtil.getRequestBody(params, false, okHttpCallback);
            Headers headers = OkHttpUtil.getRequestHeaders(params);
            Request request = OkHttpUtil.getRequest(url, requestBody, headers, null);
            call = OkHttpUtil.getOkHttpClient().newCall(request);
            call.enqueue(callback);
        } catch (Throwable e) {
            e.printStackTrace();
            callback.onFailure(call, new IOException("post", e));
        }
    }

    /**
     * 发送短信验证码
     *
     * @param presenter
     * @param mobile
     * @param okHttpListener
     */
    public static void sendVerifyCode(BasePresenter presenter, String mobile, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/SMSCode";
        OkRequestParams params = new OkRequestParams();
        params.putJson("cellphone", mobile);
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 绑定手机号
     *
     * @param presenter
     * @param mobile
     * @param verifyCode
     * @param okHttpListener
     */
    public static void verifyCode(BasePresenter presenter, String mobile, String verifyCode, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/SaveCellphone";
        OkRequestParams params = new OkRequestParams();
        params.putJson("cellphone", mobile);
        params.putJson("veriCode", verifyCode);
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 获取用户信息
     */
    public static void getUserInformation(BasePresenter presenter, OkHttpListener<BaseResponse<UserInfoBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/GetUserInfo";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }


    /**
     * 课程包列表
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getCoursePackageListData(BasePresenter presenter, OkHttpListener<BaseResponse<List<CoursePackageBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetCoursePackage";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 课程包详情
     *
     * @param presenter
     * @param coursePackageId
     * @param okHttpListener
     */
    public static void getCoursePackageDetailData(BasePresenter presenter, long coursePackageId, OkHttpListener<BaseResponse<ResponseCoursePackageDetailBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetCoursePackageInfo/";
        OkRequestParams params = new OkRequestParams();
        params.put("id", coursePackageId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 使用优惠券
     *
     * @param presenter
     * @param couponID
     * @param okHttpListener
     */
    public static void confirmUseCoupon(BasePresenter presenter, long couponID, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/UseCoupon";
        OkRequestParams params = new OkRequestParams();
        params.putJson("coupon_id", String.valueOf(couponID));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 课程详情
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void getCourseDetailData(BasePresenter presenter, long courseId, OkHttpListener<BaseResponse<CourseMVP.CourseModel.ResponseCourseDetailBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetCourseDetail/";
        OkRequestParams params = new OkRequestParams();
        params.put("id", courseId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 更新课程听课已完成状态（视频播放结束前15提交）
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void UpdateCourseCompletedStatus(BasePresenter presenter, long courseId, long record_id, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/UpdateCourseCompletedStatus";
        OkRequestParams params = new OkRequestParams();
        params.putJson("course_id", String.valueOf(courseId));
        params.putJson("record_id", String.valueOf(record_id));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 保存课程互动问答答题记录
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void AddCourseInteractiveAnswer(BasePresenter presenter, long courseId, long record_id, long questionId, int optionIndex, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/AddCourseInteractiveAnswer";
        OkRequestParams params = new OkRequestParams();
        params.putJson("CourseID", String.valueOf(courseId));
        params.putJson("CourseHistoryRecordID", String.valueOf(record_id));
        params.putJson("QuestionID", String.valueOf(questionId));
        params.putJson("AnswerIndex", String.valueOf(optionIndex));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 收藏课程
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void collectionCourse(BasePresenter presenter, long courseId, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/AddCourseFavorite";
        OkRequestParams params = new OkRequestParams();
        params.putJson("course_id", String.valueOf(courseId));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 取消收藏课程
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void cancelCollectionCourse(BasePresenter presenter, long courseId, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/DeleteCourseFavorite";
        OkRequestParams params = new OkRequestParams();
        params.putJson("course_id", String.valueOf(courseId));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 读取作业详情
     *
     * @param presenter
     * @param courseAssignmentId
     * @param courseId
     * @param okHttpListener
     */
    public static void getCourseAssignmentDetailData(BasePresenter presenter, int courseAssignmentId, int courseId, OkHttpListener<BaseResponse<CourseAssignmentBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetCourseAssignmentInfo";
        OkRequestParams params = new OkRequestParams();
        params.put("course_id", String.valueOf(courseId));
        params.put("assignment_id", String.valueOf(courseAssignmentId));
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 保存用户上传的音频文件并返回服务器端地址
     *
     * @param presenter
     * @param newFile
     * @param okHttpListener
     */
    public static void submitCourseAssignmentVideoTypeAnswer(BasePresenter presenter, File newFile, OkHttpListener<BaseResponse<CourseAssignmentVideoMVP.CourseAssignmentVideoModel.SubmitCourseAssignmentVideoAnswerBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/UploadVoice";
        OkRequestParams params = new OkRequestParams();
        params.put("File", newFile);
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }


    /**
     * 添加作业记录
     *
     * @param presenter
     * @param requestBean
     * @param okHttpListener
     */
    public static void submitCourseAssignmentVideoTypeRecord(BasePresenter presenter, RequestBean requestBean, OkHttpListener<BaseResponse<CourseAssignmentVideoMVP.CourseAssignmentVideoModel.SubmitCourseAssignmentBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/AddCourseAssignmentVideoResult";
        OkRequestParams params = new OkRequestParams();
        params.putRequestBean(requestBean);
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 读取收藏的课程列表
     *
     * @param presenter
     * @param pageNo
     * @param pageSize
     * @param okHttpListener
     */
    public static void getCourseCollectionListData(BasePresenter presenter, int pageNo, int pageSize, OkHttpListener<BaseResponse<PagerBean<CourseBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetCourseFavoriteList";
        OkRequestParams params = new OkRequestParams();
        params.put("page", pageNo);
        params.put("pagesize", pageSize);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 获取与课程相关的冥想列表
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void getMeditationListDataByCourseId(BasePresenter presenter, long courseId, OkHttpListener<BaseResponse<TrainingGroupMVP.TrainingGroupModel.TrainingGroupBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetMeditationListByCourseID";
        OkRequestParams params = new OkRequestParams();
        params.put("course_id", courseId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 获取冥想包下的冥想列表
     *
     * @param presenter
     * @param packageId
     * @param okHttpListener
     */
    public static void getMeditationListDataByPackageId(BasePresenter presenter, long packageId, OkHttpListener<BaseResponse<TrainingGroupMVP.TrainingGroupModel.TrainingGroupBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetMeditationlistByPackageID";
        OkRequestParams params = new OkRequestParams();
        params.put("package_id", packageId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 获取冥想包列表
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getTrainingPackageListData(BasePresenter presenter, OkHttpListener<BaseResponse<List<TrainingPackageBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetMeditationPackageList";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 我的优惠券列表
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getCouponListData(BasePresenter presenter, OkHttpListener<BaseResponse<List<CouponBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/MyCoupons";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 我的购买记录列表
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getPurchaseRecordListData(BasePresenter presenter, OkHttpListener<BaseResponse<List<PurchaseRecordMVP.PurchaseRecordModel.PurchaseRecordBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/MyPaymentRecord";
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 获取冥想练习详情
     *
     * @param presenter
     * @param trainingId
     * @param okHttpListener
     */
    public static void getTrainingDetailData(BasePresenter presenter, long trainingId, OkHttpListener<BaseResponse<TrainingDetailMVP.TrainingDetailModel.ResponseTrainingDetailBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetMeditationInfo";
        OkRequestParams params = new OkRequestParams();
        params.put("id", trainingId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 更新冥想完成状态
     *
     * @param presenter
     * @param recordId
     * @param okHttpListener
     */
    public static void AddMeditationRecord(BasePresenter presenter, long recordId, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/UpdateMeditationCompletedStatus";
        OkRequestParams params = new OkRequestParams();
        params.putJson("record_id", String.valueOf(recordId));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 购买会员支付
     *
     * @param presenter
     * @param okHttpListener
     */
    public static void getMemberPaymentData(BasePresenter presenter, OkHttpListener<BaseResponse<WechatPaymentBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "User/User/VipPayment";
        OkRequestParams params = new OkRequestParams();
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 根据课程包id获取对应的测试项目详情
     *
     * @param presenter
     * @param coursePackageId
     * @param okHttpListener
     */
    public static void getCourseEstimateDescriptionData(BasePresenter presenter, long coursePackageId, OkHttpListener<BaseResponse<CourseEstimateMVP.CourseEstimateModel.CourseEstimateDescriptionBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Testing/GetTestingInfoByCoursePackageID";
        OkRequestParams params = new OkRequestParams();
        params.put("package_id", coursePackageId);
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 根据id获取测试题目列表
     *
     * @param presenter
     * @param courseEstimateId
     * @param okHttpListener
     */
    public static void getCourseEstimateDetailData(BasePresenter presenter, long courseEstimateId, OkHttpListener<BaseResponse<CourseEstimateMVP.CourseEstimateModel.ResponseCourseEstimateBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Testing/GetTestingQuestions/" + courseEstimateId;
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 保存测试答题结果（每答题一道提交一次）
     *
     * @param presenter
     * @param questionID
     * @param factorGroupID
     * @param recordId
     * @param questionIndex
     * @param score
     * @param okHttpListener
     */
    public static void submitCourseEstimateAnswer(BasePresenter presenter, long questionID, long factorGroupID, long recordId, int questionIndex, int score, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Testing/SaveAnswer";
        OkRequestParams params = new OkRequestParams();
        params.putJson("QuestionID", String.valueOf(questionID));
        params.putJson("FactorGroupID", String.valueOf(factorGroupID));
        params.putJson("TestingRecordID", String.valueOf(recordId));
        params.putJson("QuestionIndex", String.valueOf(questionIndex));
        params.putJson("Result", String.valueOf(score));
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }

    /**
     * 用户测试记录详情
     *
     * @param presenter
     * @param recordId
     * @param okHttpListener
     */
    public static void getCourseEstimateReportData(BasePresenter presenter, long recordId, OkHttpListener<BaseResponse<CourseEstimateMVP.CourseEstimateModel.CourseEstimateReportBean>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Testing/GetTestingResult/" + recordId;
        OkHttpUtil.get(url, presenter, okHttpListener);
    }

    /**
     * 获取课程精选问答
     *
     * @param presenter
     * @param courseId
     * @param okHttpListener
     */
    public static void getCourseQuestionListData(BasePresenter presenter, long courseId, OkHttpListener<BaseResponse<List<CourseQuestionMVP.CourseQuestionModel.CourseQuestionBean>>> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/GetFaqListByCourseID";
        OkRequestParams params = new OkRequestParams();
        params.put("course_id", String.valueOf(courseId));
        OkHttpUtil.get(url, params, presenter, okHttpListener);
    }

    /**
     * 针对课程发表提问
     *
     * @param presenter
     * @param courseId
     * @param question
     * @param okHttpListener
     */
    public static void submitCourseQuestionData(BasePresenter presenter, long courseId, String question, OkHttpListener<BaseResponse> okHttpListener) {
        String url = Constant.getBaseApiUrl() + "Course/PostFaq";
        OkRequestParams params = new OkRequestParams();
        params.putJson("CourseID", String.valueOf(courseId));
        params.putJson("Content", question);
        OkHttpUtil.post(url, params, presenter, okHttpListener);
    }
}
